package com.union.dao;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.union.entitybean.Category;
import com.union.entitybean.Person;

/**
 * A data access object (DAO) providing persistence and search support for
 * Person entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.union.entitybean.Person
 * @author MyEclipse Persistence Tools
 */

public class PersonDAO extends HibernateDaoSupport {
	private static final Log log = LogFactory.getLog(PersonDAO.class);
	// property constants
	public static final String TITLE = "title";
	public static final String PNAME = "pname";
	public static final String SURENAME = "surename";
	public static final String TITLE_CHN = "titleChn";
	public static final String PNAME_CHN = "pnameChn";
	public static final String SURENAME_CHN = "surenameChn";

	protected void initDao() {
		// do nothing
	}

	public void save(Person transientInstance) {
		log.debug("saving Person instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Person persistentInstance) {
		log.debug("deleting Person instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Person findById(java.lang.Integer id) {
		log.debug("getting Person instance with id: " + id);
		try {
			Person instance = (Person) getHibernateTemplate().get(
					"com.test.Person", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Person instance) {
		log.debug("finding Person instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Person instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Person as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByTitle(Object title) {
		return findByProperty(TITLE, title);
	}

	public List findByPname(Object pname) {
		return findByProperty(PNAME, pname);
	}

	public List findBySurename(Object surename) {
		return findByProperty(SURENAME, surename);
	}

	public List findByTitleChn(Object titleChn) {
		return findByProperty(TITLE_CHN, titleChn);
	}

	public List findByPnameChn(Object pnameChn) {
		return findByProperty(PNAME_CHN, pnameChn);
	}

	public List findBySurenameChn(Object surenameChn) {
		return findByProperty(SURENAME_CHN, surenameChn);
	}

	public List findAll() {
		try {
			String queryString = "select person.*,personinfo.* "
				+ "from person,personinfo "
				+ "where personinfo.personId = person.personId";
		SQLQuery sqlQuery = getSession().createSQLQuery(queryString);
		return sqlQuery.list();
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public Person merge(Person detachedInstance) {
		log.debug("merging Person instance");
		try {
			Person result = (Person) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Person instance) {
		log.debug("attaching dirty Person instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Person instance) {
		log.debug("attaching clean Person instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public List findByCate(Category category) {
		int cateId = category.getCateId();
		try {
			String queryString = "select distinct pc.personId,"
					+ "person.title, person.pname, person.surename, person.title_Chn, person.pname_Chn,person.surename_Chn, "
					+ "personinfo.Membership,personinfo.Affiliation,personinfo.Ext,personinfo.Tel,personinfo.Tel2,personinfo.TelNight,personinfo.Mobile,personinfo.Fax,personinfo.Add1,personinfo.Add2,personinfo.Add3,personinfo.Email,personinfo.Email2,personinfo.Supporter,personinfo.Volunteer,personinfo.KeptInformed "
					+ "from p_c pc,person,personinfo "
					+ "where pc.cateId = "
					+ cateId
					+ " and person.personId = pc.personId and personinfo.personId = pc.personId";
			SQLQuery sqlQuery = getSession().createSQLQuery(queryString);
			return sqlQuery.list();
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public List findByCateList(String str) {
		try {
			String queryString = "select distinct pc.personId,"
					+ "person.title, person.pname, person.surename, person.title_Chn, person.pname_Chn,person.surename_Chn, "
					+ "personinfo.Membership,personinfo.Affiliation,personinfo.Ext,personinfo.Tel,personinfo.Tel2,personinfo.TelNight,personinfo.Mobile,personinfo.Fax,personinfo.Add1,personinfo.Add2,personinfo.Add3,personinfo.Email,personinfo.Email2,personinfo.Supporter,personinfo.Volunteer,personinfo.KeptInformed "
					+ "from p_c pc,person,personinfo "
					+ "where pc.cateId in ("
					+ str
					+ ") and person.personId = pc.personId and personinfo.personId = pc.personId";
			SQLQuery sqlQuery = getSession().createSQLQuery(queryString);
			return sqlQuery.list();
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public List findNotInCate() {
		try {
			String queryString = "select person.*,personinfo.* "
					+ "from person,personinfo "
					+ "where person.personId not in "
					+ "(select distinct pc.personId from p_c pc) and personinfo.personId = person.personId";
			SQLQuery sqlQuery = getSession().createSQLQuery(queryString);
			return sqlQuery.list();
		} catch (RuntimeException re) {
			throw re;
		}
	}
	
	public static PersonDAO getFromApplicationContext(ApplicationContext ctx) {
		return (PersonDAO) ctx.getBean("PersonDAO");
	}
}